//
//  OrderDetailVC.m
//  saler-ios
//
//  Created by 王亚陆 on 17/6/9.
//  Copyright © 2017年 sayimba. All rights reserved.
//

#import "OrderDetailVC.h"
#import "MyOderDetailCell.h"
#import "QiniuSDK.h"
#import "ScrollImageViewController.h"
#import <UIImageView+WebCache.h>
#import "SubmitPhotosCell.h"
@interface OrderDetailVC ()
<
UITableViewDelegate,
UITableViewDataSource,
UIScrollViewDelegate,
UIImagePickerControllerDelegate,
UINavigationControllerDelegate,
UICollectionViewDelegate,
UICollectionViewDataSource,
UIGestureRecognizerDelegate
>
{
    UIView * nav;
    UITableView * Table;
    RootViewController * TabBar;
    NSMutableDictionary * inforDic;
    UICollectionView * conllectionView;

    BOOL ImgSubmitIsOk;
    
}
@property (nonatomic,strong) UIScrollView * ScrollView;
@property (nonatomic,strong) UIView * backView;
@property (nonatomic,strong) UIView * orderBack;
@property (nonatomic,strong) UIView * priceBack;
@property (nonatomic,strong) WKProgressHUD *hud;


@property (nonatomic,strong) UILabel *imageStr;
@property (nonatomic,strong) UIButton * SubmitPhotos;
//@property (nonatomic,strong) UIButton *button1;
//@property (nonatomic,strong) UIButton *button2;
//@property (nonatomic,strong) UIButton *button3;
@property (nonatomic) NSInteger integer;
@property (nonatomic,strong) NSMutableArray *postArr;
@property (nonatomic,strong) NSMutableArray *imageArr;
@property (nonatomic,strong) NSMutableArray *btnImage;
@property (nonatomic,strong) UIView * imageBack;
@property (nonatomic,copy) NSString *token;

@end

@implementation OrderDetailVC

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.orderId = [[NSString alloc]init];
        self.HideBtnImage = [[NSString alloc]init];
        self.simbutSta = [[NSString alloc]init];
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    nav = [self createNav:@"订单详情"];
    [self.view addSubview:nav];
    self.view.backgroundColor = UIviewBackColor;
    
    inforDic = [[NSMutableDictionary alloc]init];
    self.imageArr = [[NSMutableArray alloc]init];
    self.btnImage = [[NSMutableArray alloc]init];
    self.postArr = [[NSMutableArray alloc]init];

    ImgSubmitIsOk = NO;
    
    [self createScroll];
    
    [self createDate];
    
}


-(void)updateBtnImage{
    
//    self.button1.hidden = YES;
//    self.button2.hidden = YES;
//    self.button3.hidden = YES;
    self.SubmitPhotos.hidden = YES;
    self.imageStr.hidden = YES;
    _ScrollView.contentSize = CGSizeMake(Main_Width, Main_height);
    _ScrollView.userInteractionEnabled = NO;
    
}

-(void)createScroll{
    
    
    _ScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 74*kHeight, Main_Width,Main_height-64*kHeight)];
    _ScrollView.contentOffset = CGPointMake(0, 0);
    _ScrollView.contentSize = CGSizeMake(Main_Width, 1.2*Main_height);
    _ScrollView.backgroundColor = UIviewBackColor;

    self.automaticallyAdjustsScrollViewInsets =NO;
    _ScrollView.bounces = NO;
    _ScrollView.userInteractionEnabled = YES;
    _ScrollView.showsHorizontalScrollIndicator = NO;
    _ScrollView.showsVerticalScrollIndicator = NO;
    _ScrollView.delegate = self;
    [self.view addSubview:_ScrollView];
    
    
    _backView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, Main_Width, 1.2*Main_height)];
    _backView.backgroundColor = UIviewBackColor;
    [_ScrollView addSubview:_backView];
    
    
}



-(void)createDate{
    

    NSString *urlString = [NSString stringWithFormat:@"%@/%@",myOrder_detail,self.orderId];
    
    [Common requestWithUrlString:urlString contentType:application_json finished:^(id responseObj) {

        if ([responseObj[@"success"] boolValue]) {
            
            inforDic = responseObj[@"data"];
            
            if ([inforDic[@"attachs"] count] > 0) {
                
                for (int i = 0; i<[inforDic[@"attachs"] count]; i++) {
                    [_imageArr addObject:inforDic[@"attachs"][i][@"url"]];
                    [_postArr addObject:inforDic[@"attachs"][i][@"picKey"]];
                }
            }
            
            [self createOrderBack];
            [self createPriceBack];
            [self createImage];

        } else {
            
            [WKProgressHUD popMessage:responseObj[@"message"] inView:self.view duration:1.5 animated:YES];
        }
    } failed:^(NSString *errorMsg) {
        
        NSLog(@"%@",errorMsg);
        
    }];

}

-(void)createOrderBack{
    
    
    NSString * orderStr = [NSString stringWithFormat:@"销售订单：%@",inforDic[@"code"]];
    UILabel * orderLbel = [UILabel createUIlabel:orderStr andFont:FontOfSize12 andColor:OrderColor];
    orderLbel.textAlignment = NSTextAlignmentLeft;
    [_backView addSubview:orderLbel];
    [orderLbel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_backView.mas_left).with.offset(25*kWidth);
        make.centerY.mas_equalTo(_backView.mas_top).with.offset(18*kHeight);
    }];
    

    NSString * timeStr = [NSString stringWithFormat:@"%@",inforDic[@"pickDate"]];
    timeStr = [timeStr substringToIndex:10];

    UILabel * timeLabel = [UILabel createUIlabel:timeStr andFont:FontOfSize11 andColor:OrderColor];
    [_backView addSubview:timeLabel];
    
    [timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(_backView.mas_right).with.offset(-25*kWidth);
        make.centerY.mas_equalTo(orderLbel.mas_centerY);
    }];

    _orderBack = [[UIView alloc]init];
    _orderBack.backgroundColor = WhiteColor;
    [_backView addSubview:_orderBack];
    [_orderBack mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_backView.mas_left).with.offset(10*kWidth);
        make.top.mas_equalTo(_backView.mas_top).with.offset(35*kHeight);
        make.size.mas_equalTo(CGSizeMake(Main_Width-20*kWidth, 233*kHeight));
    }];
    
    UILabel * begain = [UILabel createUIlabel:@"起始地" andFont:FontOfSize12 andColor:littleColor];
    [_orderBack addSubview:begain];
    [begain mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_orderBack.mas_left).with.offset(36*kWidth);
        make.top.mas_equalTo(_orderBack.mas_top).with.offset(19*kHeight);
    }];
    
    UIImageView * RedImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"home_yellow"]];
    [_orderBack addSubview:RedImage];
    [RedImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_orderBack.mas_left).with.offset(15*kWidth);
        make.centerY.mas_equalTo(begain.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(6*kWidth, 6*kHeight));
    }];

    
    NSString * begainStr = [NSString stringWithFormat:@"%@",inforDic[@"departRegionName"]];
    UILabel * begainAdd = [UILabel createUIlabel:begainStr andFont:FontOfSize12 andColor:littleColor];
    [_orderBack addSubview:begainAdd];
    [begainAdd mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_orderBack.mas_left).with.offset(36*kWidth);
        make.top.mas_equalTo(begain.mas_bottom).with.offset(13*kHeight);
    }];

    NSString * begainStr1 = [NSString stringWithFormat:@"%@·%@",inforDic[@"departContact"],inforDic[@"departPhone"]];
    UILabel * begainName = [UILabel createUIlabel:begainStr1 andFont:FontOfSize12 andColor:littleColor];
    [_orderBack addSubview:begainName];
    [begainName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_orderBack.mas_left).with.offset(180*kWidth);
        make.top.mas_equalTo(begain.mas_bottom).with.offset(13*kHeight);
    }];
    
    
    UIView * line1 = [[UIView alloc]init];
    line1.backgroundColor = GrayColor;
    [_orderBack addSubview:line1];
    [line1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(begain.mas_left);
        make.top.mas_equalTo(begainAdd.mas_bottom).with.offset(13*kHeight);
        make.size.mas_equalTo(CGSizeMake(369*kWidth, 1*kHeight));
    }];

    UILabel * end = [UILabel createUIlabel:@"目的地" andFont:FontOfSize12 andColor:littleColor];
    [_orderBack addSubview:end];
    [end mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_orderBack.mas_left).with.offset(36*kWidth);
        make.top.mas_equalTo(line1.mas_bottom).with.offset(19*kHeight);
    }];
    
    UIImageView * BlueImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"home_blue"]];
    [_orderBack addSubview:BlueImage];
    [BlueImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_orderBack.mas_left).with.offset(15*kWidth);
        make.centerY.mas_equalTo(end.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(6*kWidth, 6*kHeight));
    }];
    
    
    NSString * endStr = [NSString stringWithFormat:@"%@",inforDic[@"destRegionName"]];
    UILabel * endAdd = [UILabel createUIlabel:endStr andFont:FontOfSize12 andColor:littleColor];
    [_orderBack addSubview:endAdd];
    [endAdd mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_orderBack.mas_left).with.offset(36*kWidth);
        make.top.mas_equalTo(end.mas_bottom).with.offset(13*kHeight);
    }];
    
    
    NSString * endNameStr = [NSString stringWithFormat:@"%@·%@",inforDic[@"destContact"],inforDic[@"destPhone"]];
    UILabel * endName = [UILabel createUIlabel:endNameStr andFont:FontOfSize12 andColor:littleColor];
    [_orderBack addSubview:endName];
    [endName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_orderBack.mas_left).with.offset(180*kWidth);
        make.top.mas_equalTo(end.mas_bottom).with.offset(13*kHeight);
    }];
    
    UIView * line2 = [[UIView alloc]init];
    line2.backgroundColor = GrayColor;
    [_orderBack addSubview:line2];
    [line2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(endAdd.mas_left);
        make.top.mas_equalTo(endAdd.mas_bottom).with.offset(13*kHeight);
        make.size.mas_equalTo(CGSizeMake(369*kWidth, 1*kHeight));
    }];
    
    
    UILabel * carDetail = [UILabel createUIlabel:@"商品车详情" andFont:FontOfSize12 andColor:littleColor];
    [_orderBack addSubview:carDetail];
    [carDetail mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_orderBack.mas_left).with.offset(36*kWidth);
        make.top.mas_equalTo(line2.mas_bottom).with.offset(19*kHeight);
    }];
    
    UIImageView * OrderImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"myOrder"]];
    [_orderBack addSubview:OrderImage];
    [OrderImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_orderBack.mas_left).with.offset(15*kWidth);
        make.centerY.mas_equalTo(carDetail.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(7*kWidth, 7*kHeight));
    }];


    Table = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, Main_Width, 86*kHeight) style:UITableViewStylePlain];
    [Table setTableFooterView:[UIView new]];
    Table.userInteractionEnabled = NO;
    [Table setSeparatorStyle:UITableViewCellSeparatorStyleNone];

    Table.delegate = self;
    Table.dataSource = self;
    [_orderBack addSubview:Table];
    [Table mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_orderBack.mas_left);
        make.top.mas_equalTo(carDetail.mas_bottom);
        make.size.mas_equalTo(CGSizeMake(Main_Width-20*kWidth, 33*[inforDic[@"orderSkus"] count]*kHeight));
    }];
    [Table reloadData];

    [_orderBack mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(266*kHeight +33*[inforDic[@"orderSkus"] count]);
    }];


    UIView * line3 = [[UIView alloc]init];
    line3.backgroundColor = GrayColor;
    [_orderBack addSubview:line3];
    [line3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(endAdd.mas_left);
        make.top.mas_equalTo(Table.mas_bottom).with.offset(5*kHeight);
        make.size.mas_equalTo(CGSizeMake(369*kWidth, 1*kHeight));
    }];
    
    
    UILabel * beizhu = [UILabel createUIlabel:@"备注" andFont:FontOfSize12 andColor:littleColor];
    [_orderBack addSubview:beizhu];
    [beizhu mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_orderBack.mas_left).with.offset(36*kWidth);
        make.top.mas_equalTo(line3.mas_bottom).with.offset(19*kHeight);
    }];
    
    UIImageView * beizhuImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"beizhudetail"]];
    [_orderBack addSubview:beizhuImage];
    [beizhuImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_orderBack.mas_left).with.offset(15*kWidth);
        make.centerY.mas_equalTo(beizhu.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(7*kWidth, 7*kHeight));
    }];


    NSString * comment = [NSString stringWithFormat:@"销售备注  %@",[[Common shared] backString:inforDic[@"comment"]]];
    if (comment.length == 0 ||[comment isEqualToString:@"销售备注  "]) {
        comment = @"销售备注  无";
    }
    
    UILabel * beizhu1 = [UILabel createUIlabel:comment andFont:FontOfSize12 andColor:OrderColor];
    beizhu1.textAlignment = NSTextAlignmentLeft;
    beizhu1.backgroundColor = RGBACOLOR(248, 248, 248, 1);
    [_orderBack addSubview:beizhu1];
    [beizhu1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_orderBack.mas_left).with.offset(36*kWidth);
        make.top.mas_equalTo(beizhu.mas_bottom).with.offset(10*kHeight);
//        make.size.mas_equalTo(CGSizeMake(7*kWidth, 7*kHeight));
    }];
    

    int Height = 0;
    
    if (beizhu1.frame.size.width > Main_Width) {
        
        beizhu1.numberOfLines = 3;
        Height = 1;
        [beizhu1 mas_updateConstraints:^(MASConstraintMaker *make) {
            
            make.size.mas_equalTo(CGSizeMake(Main_Width -36*2*kWidth, 66*kHeight));
            
        }];
    }
    
    //auditStatus = 30 未通过
    NSString * auditStatus = [NSString stringWithFormat:@"%@",inforDic[@"auditStatus"]];
    if ([auditStatus isEqualToString:@"30"]) {
        
        NSString * auditComment = [NSString stringWithFormat:@"审核备注  %@",inforDic[@"auditComment"]];
        UILabel * auditStatusLabel = [UILabel createUIlabel:auditComment andFont:FontOfSize12 andColor:OrderColor];
        auditStatusLabel.textAlignment = NSTextAlignmentLeft;
        auditStatusLabel.backgroundColor = RGBACOLOR(248, 248, 248, 1);
        [_orderBack addSubview:auditStatusLabel];
        [auditStatusLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(_orderBack.mas_left).with.offset(36*kWidth);
            make.top.mas_equalTo(beizhu1.mas_bottom).with.offset(10*kHeight);
        }];
        
        [_orderBack mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(290*kHeight +Height *33+33*[inforDic[@"orderSkus"] count]);
        }];
    }

}

-(void)createPriceBack{
    
    UILabel * price = [UILabel createUIlabel:@"价格" andFont:FontOfSize12 andColor:OrderColor];
    [_backView addSubview:price];
    [price mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_backView.mas_left).with.offset(25*kWidth);
        make.centerY.mas_equalTo(_orderBack.mas_bottom).with.offset(18*kHeight);
    }];

    _priceBack = [[UIView alloc]init];
    _priceBack.backgroundColor = WhiteColor;
    [_backView addSubview:_priceBack];
    [_priceBack mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_backView.mas_left).with.offset(10*kWidth);
        make.top.mas_equalTo(_orderBack.mas_bottom).with.offset(35*kHeight);
        make.size.mas_equalTo(CGSizeMake(Main_Width-20*kWidth, 100*kHeight));
    }];
    
    UILabel * biaozhun = [UILabel createUIlabel:@"标准价格" andFont:FontOfSize12 andColor:littleColor];
    [_priceBack addSubview:biaozhun];
    [biaozhun mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_priceBack.mas_left).with.offset(36*kWidth);
        make.centerY.mas_equalTo(_priceBack.mas_top).with.offset(28*kHeight);
    }];
    
    NSString * star = [NSString stringWithFormat:@"%@元",inforDic[@"standardSalePrice"]];
    UILabel * price1 = [UILabel createUIlabel:star andFont:FontOfSize13 andColor:RedColor];
    [_priceBack addSubview:price1];
    [price1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(_priceBack.mas_right).with.offset(-15*kWidth);
        make.centerY.mas_equalTo(_priceBack.mas_top).with.offset(28*kHeight);
    }];

    
    
    UIImageView * biaozhunImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"biaozhun"]];
    [_priceBack addSubview:biaozhunImage];
    [biaozhunImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_priceBack.mas_left).with.offset(15*kWidth);
        make.centerY.mas_equalTo(biaozhun.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(15*kWidth,15*kHeight));
    }];

    UILabel * shiji = [UILabel createUIlabel:@"实际价格" andFont:FontOfSize12 andColor:littleColor];
    [_priceBack addSubview:shiji];
    [shiji mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_priceBack.mas_left).with.offset(36*kWidth);
        make.centerY.mas_equalTo(_priceBack.mas_bottom).with.offset(-28*kHeight);
    }];
    
    NSString * suggestSalePrice = [NSString stringWithFormat:@"%@元",[[Common shared] backString:inforDic[@"suggestSalePrice"]]];

    UILabel * price2 = [UILabel createUIlabel:suggestSalePrice andFont:FontOfSize13 andColor:RedColor];
    [_priceBack addSubview:price2];
    [price2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(_priceBack.mas_right).with.offset(-15*kWidth);
        make.centerY.mas_equalTo(_priceBack.mas_bottom).with.offset(-28*kHeight);
    }];

    
    UIImageView * shijiImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"shiji"]];
    [_priceBack addSubview:shijiImage];
    [shijiImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_priceBack.mas_left).with.offset(15*kWidth);
        make.centerY.mas_equalTo(shiji.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(15*kWidth, 15*kHeight));
    }];

    UIView * line = [[UIView alloc]init];
    line.backgroundColor = GrayColor;
    [_priceBack addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_priceBack.mas_left);
        make.centerY.mas_equalTo(_priceBack.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(Main_Width-20*kWidth, 1*kHeight));
    }];


    
}


-(void)createImage{
    
    _imageStr = [UILabel createUIlabel:@"支付详情" andFont:FontOfSize12 andColor:OrderColor];
    [_backView addSubview:_imageStr];
    [_imageStr mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_backView.mas_left).with.offset(25*kWidth);
        make.centerY.mas_equalTo(_priceBack.mas_bottom).with.offset(18*kHeight);
    }];
    
    
    _imageBack = [[UIView alloc]init];
    _imageBack.backgroundColor = WhiteColor;
    [_backView addSubview:_imageBack];
    [_imageBack mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_backView.mas_left).with.offset(10*kWidth);
        make.top.mas_equalTo(_priceBack.mas_bottom).with.offset(35*kHeight);
        make.size.mas_equalTo(CGSizeMake(Main_Width-20*kWidth, (50+80)*kHeight));
    }];
    
    
    UILabel * imgLabel = [UILabel createUIlabel:@"图片" andFont:FontOfSize13 andColor:littleColor];
    [_imageBack addSubview:imgLabel];
    [imgLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_backView.mas_left).with.offset(25*kWidth);
        make.centerY.mas_equalTo(_imageBack.mas_top).with.offset(18*kHeight);
    }];
    
    
    UIImageView * img = [[UIImageView alloc]init];
    img.image = [UIImage imageNamed:@"xiangji"];
    [_imageBack addSubview:img];
    [img mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(_imageBack.mas_right).with.offset(-10*kWidth);
        make.centerY.mas_equalTo(imgLabel.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(17*kWidth, 15*kHeight));
    }];

    
    UIButton * imgBtn = [UIButton createUIbutton:@"" andFont:FontOfSize13 andColor:nil];
    imgBtn.tag = 100;
//    [imgBtn setImage:[UIImage imageNamed:@"xiangji"] forState:UIControlStateNormal];
    [imgBtn addTarget:self action:@selector(pressImgBtn) forControlEvents:UIControlEventTouchUpInside];
//    imgBtn.backgroundColor = CyanColor;
    [_imageBack addSubview:imgBtn];
    [imgBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(_imageBack.mas_right).with.offset(-5*kWidth);
        make.centerY.mas_equalTo(imgLabel.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(60*kWidth, 30*kHeight));
    }];
    
    UIView * Hline =[[UIView alloc]init];
    Hline.backgroundColor = GrayColor;
    [_imageBack addSubview:Hline];
    [Hline mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(_imageBack.mas_centerX);
        make.top.mas_equalTo(imgLabel.mas_bottom).with.offset(15*kHeight);
        make.size.mas_equalTo(CGSizeMake(Main_Width-20*kWidth, 1*kHeight));
    }];
    

    
    
    
    
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.itemSize = CGSizeMake(90*kWidth , 38*kHeight );
    
    conllectionView =  [[UICollectionView alloc]initWithFrame:CGRectMake(0, cellHeight * 7 + 20, screenWidth,  (cellHeight + 130 )) collectionViewLayout:layout];
    conllectionView.backgroundColor = WhiteColor;
    conllectionView.delegate = self;
    conllectionView.dataSource = self;
    [conllectionView registerNib:[UINib nibWithNibName:@"SubmitPhotosCell" bundle:nil] forCellWithReuseIdentifier:@"cell"];
    
    [self.view addSubview:conllectionView];
    [conllectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_backView.mas_left).with.offset(10*kWidth);
        make.top.mas_equalTo(Hline.mas_bottom).with.offset(5*kHeight);
        make.right.mas_equalTo(_backView.mas_right).with.offset(-10*kWidth);
        make.height.mas_equalTo(80*kHeight);
    }];

    
    [self updateConllectViewFram];
    
    
    if ([self.HideBtnImage isEqualToString:@"1"]) {

        conllectionView.hidden = YES;
        _imageBack.hidden = YES;
    }
    
    

    
    
    


    _SubmitPhotos = [UIButton buttonWithType:UIButtonTypeCustom];
    [_SubmitPhotos setTitle:@"提交照片" forState:UIControlStateNormal];
    [_SubmitPhotos setTitleColor:WhiteColor forState:UIControlStateNormal];
    [_SubmitPhotos addTarget:self action:@selector(pressSubmit) forControlEvents:UIControlEventTouchUpInside];
    LRViewBorderRadius(_SubmitPhotos, 5, 0.5, YellowColor);
    _SubmitPhotos.backgroundColor = YellowColor;
    [_ScrollView addSubview:_SubmitPhotos];
    [_SubmitPhotos mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(_ScrollView.mas_centerX);
        make.size.mas_equalTo(CGSizeMake(355*kWidth, 50*kHeight));
        make.top.mas_equalTo(conllectionView.mas_bottom).with.offset(20*kHeight);
    }];

    
    if ([_HideBtnImage isEqualToString:@"1"]) {
        [self updateBtnImage];
    }
    
   
    if ([_simbutSta isEqualToString:@"30"]) {
        
        
        [self updateBtnNot];
        conllectionView.userInteractionEnabled = YES;
        
    }else{
        UILongPressGestureRecognizer *tap = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressToDo:)];
        [conllectionView addGestureRecognizer:tap];

    }
    
    
}


-(void)longPressToDo:(UILongPressGestureRecognizer *)gestureRecognizer
{
    if (gestureRecognizer.state != UIGestureRecognizerStateEnded) {
        return;
    }
    CGPoint p = [gestureRecognizer locationInView:conllectionView];
    
    NSIndexPath *indexPath = [conllectionView indexPathForItemAtPoint:p];
    if (indexPath == nil){
        NSLog(@"couldn't find index path");
    } else {
        
        
        // get the cell at indexPath (the one you long pressed)
        UICollectionViewCell* cell =
        [conllectionView cellForItemAtIndexPath:indexPath];
        
        
        [self tapRemovedPicture:indexPath.item];
        // do stuff with the cell
        NSLog(@"%ld",(long)indexPath.item);
    }
}



-(void)pressImgBtn{
    
    
    
    if (_imageArr.count == 6) {
      
        [WKProgressHUD popMessage:@"最多上传6张照片" inView:self.view duration:1.5 animated:YES];
        
    }else{
        [self getPictureFromLibrary];
    }
    
    
}


-(void)updateBtnNot{
    

    conllectionView.userInteractionEnabled = NO;
    
    UIButton * btn = [_imageBack viewWithTag:100];
    btn.userInteractionEnabled = NO;
    
    self.SubmitPhotos.hidden = YES;
    
}

-(void)pressSubmit{
    
    
    NSMutableArray * arr = [[NSMutableArray alloc]init];
    for (int i = 0; i<_postArr.count; i++) {
        
        [arr addObject:self.postArr[i]];
        
    }
    
    //SubmitPhotos_url
    NSData * jsonData = [NSJSONSerialization dataWithJSONObject:arr options:NSJSONWritingPrettyPrinted error:nil];
    NSString * jsonString = [[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding];
    
    NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
    [dic setObject:jsonString forKey:@"attachs"];
    [dic setObject:inforDic[@"code"] forKey:@"saleOrderCode"];
    [dic setObject:inforDic[@"id"] forKey:@"saleOrderId"];

    
    [[Common shared] afPostRequestWithUrlString:SubmitPhotos_url parms:dic finishedBlock:^(id responseObj) {
        
        NSDictionary *dicInfor = [NSJSONSerialization JSONObjectWithData:responseObj options:NSJSONReadingMutableLeaves error:nil];
        
        [self.hud dismiss:YES];
        
        
        [WKProgressHUD popMessage:dicInfor[@"message"] inView:self.view duration:1.5 animated:YES];
        NSLog(@"%@",dicInfor[@"message"]);

        
        
    } failedBlock:^(NSString *errorMsg) {
        
        [self.hud dismiss:YES];
        
    }];

    
}


//删除图片
-(void)tapRemovedPicture:(NSInteger)index
{

    UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    
    
    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"删除" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

        [_imageArr removeObjectAtIndex:index];
        [_postArr removeObjectAtIndex:index];
        
        
        
        [self updateConllectViewFram];
        
        
        
    }];
    
    [alert addAction:action1];
    [alert addAction:action2];
    
    [self presentViewController:alert animated:YES completion:nil];
}


-(void)updateConllectViewFram{
    
    if ([_imageArr count] >3) {
        [conllectionView mas_updateConstraints:^(MASConstraintMaker *make) {
            
            make.height.mas_equalTo(160*kHeight);
            
        }];
        
    }else if ([_imageArr count] == 0){
        [conllectionView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(0.1*kHeight);
        }];
        
    }else{
        [conllectionView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(80*kHeight);
        }];
    }
    
    
    [conllectionView reloadData];

    
}


//获取图片
-(void)tapSelectPicture
{
    __weak typeof(self) weakSelf = self;
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];

    
    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"从相册选取" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [weakSelf getPictureFromLibrary];
        
    }];
    
    [alert addAction:action1];
    [alert addAction:action2];
    
    [self presentViewController:alert animated:YES completion:nil];
}



#pragma mark 从相册选取
-(void)getPictureFromLibrary
{
    UIImagePickerController *picker = [[UIImagePickerController alloc]init];
    picker.delegate = self;
    picker.allowsEditing = YES;
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    
    [self presentViewController:picker animated:YES completion:nil];
    
}

#pragma mark UIImagePickerControllerDelegate
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
    [picker dismissViewControllerAnimated:YES completion:^{
        
        UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
        [_imageArr addObject:image];
        
        ImgSubmitIsOk = YES;
        

        
        [self updateConllectViewFram];
        
        
//        if (_integer == 1) {
//            [self.button1 setImage:image forState:UIControlStateNormal];
//            [self.imageArr replaceObjectAtIndex:0 withObject:image];
//        } else  if(_integer == 2){
//            [self.button2 setImage:image forState:UIControlStateNormal];
//            [self.imageArr replaceObjectAtIndex:1 withObject:image];
//
//        } else {
//            
//            [self.button3 setImage:image forState:UIControlStateNormal];
//            [self.imageArr replaceObjectAtIndex:2 withObject:image];
//
//        }
        
        self.hud  = [WKProgressHUD showInView:self.view withText:@"" animated:YES];
        
        
        //        NSIndexPath *index = [NSIndexPath indexPathForRow:0 inSection:0];
        //        MineCell *cell = [self.tableView cellForRowAtIndexPath:index];
        //
        //        cell.ImageView.image = image;
        
        //上传服务器
        [self getTokenFromQN];
        
    }];
}



#pragma mark 从七牛获取Token
-(void)getTokenFromQN
{
    
    [Common requestWithUrlString:qiniu_token contentType:application_json finished:^(id responseObj) {
        if ([responseObj[@"success"] boolValue]) {
   
            

            
            self.token = responseObj[@"data"];
            [self uploadImageToQNWithData:[self getImageWith:[_imageArr lastObject]]];
            //
            //            NSIndexPath *index = [NSIndexPath indexPathForRow:0 inSection:0];
            //            MineCell *cell = [self.tableView cellForRowAtIndexPath:index];
            //if
//            if (_integer == 1) {
//                
//                [self uploadImageToQNWithData:[self getImageWith:self.button1.imageView.image]];
//                
//            } else if(_integer == 2){
//                [self uploadImageToQNWithData:[self getImageWith:self.button2.imageView.image]];
//                
//            } else {
//                [self uploadImageToQNWithData:[self getImageWith:self.button3.imageView.image]];
//
//            }
            
        }
    } failed:^(NSString *errorMsg) {
        
        [self.hud dismiss:YES];
        
        NSLog(@"%@",errorMsg);
    }];
    
    
    
}

-(NSData *)getImageWith:(UIImage *)image
{
    NSData *data = nil;
    if (UIImagePNGRepresentation(image)) {
        
        data = UIImageJPEGRepresentation(image, 0.3);
        
        
    } else {
        data = UIImagePNGRepresentation(image);
        
        
    }
    
    return data;
}

-(void)uploadImageToQNWithData:(NSData *)data
{
    
    QNUploadManager *upManager = [[QNUploadManager alloc]init];
    
    QNUploadOption *uploadOption = [[QNUploadOption alloc]initWithMime:nil progressHandler:^(NSString *key, float percent) {
        
    } params:nil checkCrc:NO cancellationSignal:nil];
    
    [upManager putData:data key:nil token:self.token complete:^(QNResponseInfo *info, NSString *key, NSDictionary *resp) {
        

        [_postArr addObject:resp[@"key"]];

        
        [self.hud dismiss:YES];
        
        
    } option:uploadOption];
}





#pragma mark - 表单协议

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView
 numberOfRowsInSection:(NSInteger)section{
    
    return [inforDic[@"orderSkus"] count];
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString  *CellIdentifier = @"CellIdentifier";
    
    MyOderDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if (cell == nil) {
        
        cell = [[MyOderDetailCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

    }
    
    
    cell.nameLabel.text = [NSString stringWithFormat:@"%@-%@",[[Common shared] backString:inforDic[@"orderSkus"][indexPath.row][@"brandName"]],inforDic[@"orderSkus"][indexPath.row][@"seriesName"]];
    cell.numberLabel.text = [NSString stringWithFormat:@"%@辆",[[Common shared] backString:inforDic[@"orderSkus"][indexPath.row][@"amt"]]];
    cell.nameLabel.textColor = littleColor;
    cell.numberLabel.textColor = littleColor;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView
heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return  NewCellHeight;
}

- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    
    
}


#pragma mark UICollectionView Delegate DataSource

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    
    return 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _imageArr.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    
    static NSString * cellIde=@"cell";
    //因为提前注册了cell CollectionView一旦从重用队列中拿不到cell对象，会自动创建对应样式的cell
    SubmitPhotosCell * cell=[collectionView dequeueReusableCellWithReuseIdentifier:cellIde forIndexPath:indexPath];
    
    
    //rangeOfString
    NSString * str = [NSString stringWithFormat:@"%@",_imageArr[indexPath.item]];
    if ([str rangeOfString:@"http"].location !=NSNotFound) {
        
        [cell.submitImage sd_setImageWithURL:[NSURL URLWithString:_imageArr[indexPath.item]] placeholderImage:[UIImage imageNamed:@""]];
 
    }else{
        cell.submitImage.image = _imageArr[indexPath.item];
    }
    

    
    
    return cell;
}


-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    return CGSizeMake(70*kWidth , 70*kHeight );
}




-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    //top, left, bottom, right
    return UIEdgeInsetsMake(5, 20, 5, 35);
}


-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    ScrollImageViewController *scrollVC = [[ScrollImageViewController alloc]init];
    scrollVC.imageArr = _imageArr;
    [self.navigationController pushViewController:scrollVC animated:YES];
    

}


- (BOOL)collectionView:(UICollectionView *)collectionView shouldShowMenuForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    
    RootViewController *rootVC = [RootViewController defaultsTabBar];
    [rootVC setTabBarHidden:YES];
    
}

@end
